import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class VideoView extends StatefulWidget {
  final String url;
  final String cover;
  final bool autoPlay;
  final bool looping;
  final double aspectRation;

  const VideoView(
      {super.key,
      required this.url,
      this.cover = "",
      this.autoPlay = false,
      this.looping = false,
      this.aspectRation = 16 / 9});

  @override
  State<VideoView> createState() => _VideoViewState();
}

class _VideoViewState extends State<VideoView> {
  late ChewieController _chewieController;
  late VideoPlayerController _playerController;

  @override
  void initState() {
    super.initState();
    _playerController = VideoPlayerController.networkUrl(Uri.parse(widget.url));
    _chewieController = ChewieController(
        videoPlayerController: _playerController,
        aspectRatio: widget.aspectRation,
        autoPlay: widget.autoPlay,
        looping: widget.looping);
  }

  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    double playerHeigth = screenWidth / widget.aspectRation;
    return SizedBox(
      width: screenWidth,
      height: playerHeigth,
      child: Chewie(controller: _chewieController),
    );
  }
}
